//https://blog.csdn.net/weixin_42289080/article/details/130725695
import {defineStore} from 'pinia'
import {generatorDynamicRouter,loadView} from '../router/RouterUtil';
import userUser  from "./userUser.js";
import layoutLang from "../language/lang/impl/layout/layoutLang"
import {I18Parse} from '@lowcode/jpaas-base-lib';
import SysAppApi from "@/common/api/system/sysApp";
//mapStores  mapState  mapActions
//storeToRefs  $patch
//参数一：容器id必须唯一;
export default defineStore('main', {
    state: () => {//必须是箭头函数
        return {
            layout:'UpDownLayout',//当前的布局类型；
            logined:false,//登录状态
            hideArea:false,//隐藏头部与左侧区域
            menus:{}, //菜单
            routers:[], //动态获取的路由
            curApp:{},//当前的应用对象
            appKey:"", //当前应用标识
            navigations:[], //保存页签
            navigation:"", //当前选中的页签key
            selectedMenuKey:"", //选择菜单的key
            openMenuKeys:[], //当前展开的菜单
            buttons: {},//有权限的按钮
            allButtons: {},//所有的按钮
            haveHomePage:"", //应用是否有首页
            curAppPermission:false,
            //是否微前端打开
            isMicroApp:false,
            //授权类型 0,没有权限,1,开发权限 2.管理权限
            authType:0
        }
    },
    //类似组件的computed,有缓存的功能
    getters: {
        getAppKey() {
            return this.appKey;
        }
    },
    //类似组件的methods  封装业务逻辑  修改state
    actions: {
        setIsMicroApp(isMicroApp){
            this.isMicroApp = isMicroApp ;
        },
        setLayout(type){
            this.layout = type ;
        },
        setLogined(logined) {
            this.logined = logined;
        },
        setHideArea(hideArea) {
            this.hideArea = hideArea;
        },
        setMenus(menus) {
            this.menus = menus;
        },
        setRouters( routers){
            this.routers = routers;
        },
        setAppKey(appKey){
            this.appKey = appKey;
        },
        async setCurApp(curApp){
            this.curApp = curApp;
        },
        getCurAppName(){
            let title=I18Parse.getI18Val(layoutLang.layoutLang, "homeTitle", "敏捷开发平台");
            if(this.curApp && this.curApp.clientName ){
                title= this.curApp.clientName;
            }
            return title;
        },
        setNavigations(navigations){
            this.navigations = navigations;
        },
        setNavigation(navigation){
            this.navigation = navigation;
        },
        setSelectedMenuKey(selectedMenuKey){
            this.selectedMenuKey = selectedMenuKey;
        },
        setOpenMenuKeys(openMenuKeys){
            this.openMenuKeys = openMenuKeys;
        },
        setButtons(buttons){
            this.buttons = buttons;
        },
        setAllButtons(allButtons){
            this.allButtons = allButtons;
        },
        setHaveHomePage(haveHomePage){
            this.haveHomePage=haveHomePage;
        },
        setAuthType(authType_){
            this.authType=authType_;
        },
        /**
         * 构建路由
         * @returns {Promise<any>}
         * @constructor
         */
        generateRoutes(component) {
            return new Promise(resolve => {
                let modules=import.meta.glob('@/views/**/*.vue');
                generatorDynamicRouter(this.appKey,component,modules).then(res => {
                    //根据当前应用的ID 获取菜单
                    let curApp = res.sysApp;


                    this.setLogined(true);
                    this.setMenus( res.menus);
                    this.setCurApp(curApp);
                    this.setRouters( res.routers);
                    this.setHaveHomePage(res.haveHomePage);
                    this.setAuthType(res.authType)
                    sessionStorage.setItem("CUR_APP_KEY",this.appKey);
                    //当前用户
                    userUser().setUser(res.user);
                    resolve(res.routers)
                })
            })
        },
        //获取存入到sessionStorage 的属性
        getSessionStorage(){
            return {
                appKey:this.appKey,
                curApp:this.curApp,
                navigations:this.navigations,
                navigation:this.navigation,
                selectedMenuKey:this.selectedMenuKey,
                openMenuKeys:this.openMenuKeys,
                buttons: this.buttons,
                allButtons: this.allButtons,
                haveHomePage:this.haveHomePage,
            }
        },
        /**
         * 根据菜单key 获取菜单获取父级菜单.
         * @param menuKey   菜单KEY.
         * @returns {*[]}
         */
        getMenusByMenuKey(menuKey){
            let menus = this.menus;
            let menuAry = [];
            let menu = menus.find(item=>item.menuKey === menuKey);
            if(menu ){
                menuAry.push(menu);
            }
            while(menu && menu.parentId != '0'){
                menu = menus.find(item=>item.id === menu.parentId);
                if(menu ){
                    menuAry.unshift(menu);
                }
            }
            return menuAry;

        }
    }
})




